layui.define(function (exports) {
	var object = {
			c: {data:[]}, // 配置项
			f: function (c) { // 数据初始化
				var s=new Date().getTime(),t = this;
				t.c.id = c.id?c.id:'simple_table_view_before_create_order';
				t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
				//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
				t.c.data = c.data; // 数据
				t.c.bid = c.bid; // 采购订单号
			},
	        load:function () { // 加载控件
	        	var h = ''
	            	+ '<div class="layui-form-item" pane>'
	            		+ '<label class="layui-form-label">优惠情况</label>'
	            		+ '<div class="layui-input-block" id="'+this.c.tableId+'">'
	            			+ this.loadTable()
	            		+ '</div>'
	        		+ '</div>'
        		+ '</div>';
	            $('#'+this.c.id).html(h);
	            this.listenBtn();
	        },
	        loadTable:function () { // 加载表格
	            return '<table class="layui-table" style="width: 98%;" lay-skin="" style="margin:0"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
	        },
	        loadTableOfThead:function () { // 加载表头
	            var h = '<thead><tr style="background-color: #0087fa66;">';
	            h += '<th style="width:18%; text-align: center;">标题</th>';
	            h += '<th style="width:18%; text-align: center;">优惠金额</th>';
	            h += '<th style="width:18%; text-align: center;">操作</th></tr></thead>';
				return h;
	        },
	        loadTableOfTbody:function () { // 加载表tbody
	        	var t = this;
	        	var h = '<tbody>';
	            for(var i=0;i<t.c.data.length;i++){
		            h += '<tr>';
		            h += '<td style="text-align:center;">' + t.c.data[i].text + '</td>';
		            h += '<td style="text-align:center;">' + (t.c.data[i].discountFee / 100) + '</td>';
	                h += '<td style="text-align:center;"><a class="layui-btn layui-btn-sm" id="id_' + new Date().getTime() + '_' + i + '" data-index="'+i+'" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>设定默认</a></td>'
	                h+= '</tr>';
	            }
                
	            h += '</tbody>';
				return h;
	        },
	        listenBtn:function () { // 添加监听
	            var t = this;
	            // 监听“设定默认”
	            $('#' + t.c.tableId).find('a[lay-filter="edit"]').on('click',function (e) {
	                var i = $(this).attr('data-index');
	                
					$.ajax({
						type: "POST",
						url: '/purchase/purchaseorder/updateAliPromotionId',
						data: {
							bid: t.c.bid,
							aliPromotionId: t.c.data[i]['promotionId']
						},
						success: function (data) {
							if (data.code == 0) {
								layer.msg(data.msg);
							} else {
								alert(data.msg);
							}
						}
					});
	            });
	        },
	        reloadTable:function () { // 重新加载表格
	            $('#'+this.c.tableId).html(this.loadTable());
	            this.listenBtn();
	        },
	        getData:function () {
	            return this.c.data;
	        },
	        init:function (c) {this.f(c);this.load();return object;},
	        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
	        addRow:function (row) {
	        	var c = this.c;
	        	c.data.push(row);
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        addRows:function (rows) {
	        	var c = this.c;
	        	for (var i = 0; i < rows.length; i++) {
		        	c.data.push(rows[i]);
				}
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        editRow:function (index, row) {
	        	var c = this.c;
				c.data[index] = row;
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        reloadRows:function (rows) {
	        	var c = this.c;
	        	c.data = rows;
	        	return this.reload(c);
	        }
		};
	exports('simple_table_view_before_create_order', object);
});